<?php

namespace controller;

/**
 * Description of ShopAdmin_Customer
 *
 * @author Damian
 */
class ShopAdmin_Customer extends Seller {

    public function index() {
        $layoutView = $this->getLayoutView();
        $layoutView->set("paneltitle", "Lista klientów");
        $view = $this->getActionView();
        $letters = ['A', 'Ą', 'B', 'C', 'Ć', 'D', 'E', 'Ę', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'Ł', 'M', 'N', 'O', 'Ó', 'P', 'R', 'S', 'T', 'U', 'W', 'X', 'Y', 'Z', 'Ź', 'Ż'];
        $view->set("letters", $letters);
        $view->set("shop", $this->shop);
        $customersCriteria = new \model\Criteria();
        $customersCriteria->from(\model\TblCustomer::getModel()->getTable())
                ->where(\model\TblCustomer::SHOP_ID, "=", $this->shop->ShopID);
        $page = (\library\RequestMethods::get("Page")) ? \library\RequestMethods::get("Page") : 1;
        $all = $customersCriteria->count();
        $pagesCount = ceil($all / $this->perPage);
        $pages = new \view\Pagination($page, 1, $pagesCount, "./" . $this->shop->URL . "/" . "ShopAdmin_Customer");
        $layoutView->set("pages", $pages);
        $customersCriteria->join(\model\TblPerson::getModel());
        if (\library\RequestMethods::get("letter")) {
            $customersCriteria->where(\model\TblPerson::SURNAME, " like ", \library\RequestMethods::get("letter") . "%", false, " and ");
        }
        if (\library\RequestMethods::get("id")){
            $customersCriteria->where(\model\TblCustomer::ID, "=", \library\RequestMethods::get("id"),  \model\TblCustomer::getModel()->getTable()," and ");
        }
        //var_dump($customersCriteria->getSelectQuery());
        $customers = \model\TblCustomer::getModel()->all($customersCriteria);
        $customersDataTable = new \model\DataTable($customers);
        $customersData = $customersDataTable->getModelData(true);
        foreach ($customersData as &$customer) {
            //login data
            $logLoginCriteria = new \model\Criteria();
            $logLoginCriteria->from(\model\TblLogLogin::getModel()->getTable())
                    ->where(\model\TblLogLogin::PERSON_ID, "=", $customer->PersonID->PersonID)
                    ->where("DateDiff(curdate()," . \model\TblLogLogin::LOGIN_DATE . ")", "<", 30, false, " and ", false);
            $logLogin = new \model\DataTable(\model\TblLogLogin::getModel()->all($logLoginCriteria));
            $customer->logins = $logLogin->getModelData(true);
            //opinion data
            $opinionsCriteria = new \model\Criteria();
            $opinionsCriteria->from(\model\TblOpinion::getModel()->getTable())
                    ->join(\model\TblCustomer::getModel())
                    ->join(\model\TblProduct::getModel())
                    ->order(\model\TblOpinion::ADDITION_DATE, " desc ", \model\TblOpinion::getModel()->getTable())
                    ->where(\model\TblOpinion::SHOP_ID, "=", $this->shop->ShopID, \model\TblOpinion::getModel()->getTable())
                    ->where(\model\TblOpinion::CUSTOMER_ID, "=", $customer->CustomerID, \model\TblOpinion::getModel()->getTable(), " and ")
                    ->where("DateDiff(curdate(),`tblopinion`." . \model\TblOpinion::ADDITION_DATE . ")", "<", 30, false, " and ", false);
            $opinions = \model\TblOrder::getModel()->all($opinionsCriteria);
            $opinionsDataTable = new \model\DataTable($opinions);
            $opinionsTable = $opinionsDataTable->getModelData(true);
            $customer->opinions = $opinionsTable;
            //orders
            $orderCriteria = new \model\Criteria();
            $orderCriteria->from(\model\TblOrder::getModel()->getTable())
                    ->join(\model\TblDeliveryMethod::getModel())
                    ->join(\model\TblPayment::getModel())
                    ->order(\model\TblOrder::ORDER_DATE, " desc ")
                    ->join(\model\TblCustomer::getModel())
                    ->where(\model\TblOrder::CUSTOMER_ID, "=", $customer->CustomerID, \model\TblOrder::getModel()->getTable());
            $orders = \model\TblOrder::getModel()->all($orderCriteria->limit($this->perPage, $page));
            $ordersDataTable = new \model\DataTable($orders);
            $ordersTable = $ordersDataTable->getModelData(true);
            foreach ($ordersTable as &$order) {
                $productsCriteria = new \model\Criteria();
                $productsCriteria->from(\model\TblProduct::getModel()->getTable())
                        ->join(\model\TblProductOrder::getModel())
                        ->where(\model\TblProductOrder::ORDER_ID, "=", $order->OrderID);
                $products = \model\TblProductOrder::getModel()->all($productsCriteria);
                $productsTable = new \model\DataTable($products);
                $order->Products = $productsTable->getModelData(true);
            }
            $customer->orders = $ordersTable;

            $adressCriteria = new \model\Criteria();
            $adressCriteria->from(\model\TblAdress::getModel()->getTable())
                    ->join(\model\TblPerson::getModel())
                    ->join(\model\TblStreet::getModel())
                    ->join(\model\TblCity::getModel())
                    ->join(\model\TblPostCode::getModel())
                    ->where(\model\TblPersonAdress::PERSON_ID, "=", $customer->PersonID->PersonID, \model\TblAdress::getModel()->getTable());
            $adressesModelData = new \model\DataTable( \model\TblPersonAdress::getModel()->all($adressCriteria), false);
            $adresses = $adressesModelData->getModelData(true);
            $customer->adresses = $adresses;
        }
        $view->set("customers", $customersData);
        $this->render();
        // var_dump($customersData);
    }

}
